"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require('@angular/core');
var forms_1 = require('@angular/forms');
var BearingAddComponent = (function () {
    function BearingAddComponent(fb) {
        this.fb = fb;
        this.bear = {
            id: '',
            types: '',
            brand: '',
            new_style: '',
            old_style: '',
            inside_diameter: NaN,
            outside_diameter: NaN,
            thickness: NaN,
            purchase_price: NaN,
            unit_price: NaN,
            stock: NaN,
            arrival_data: ''
        };
        this.active = true;
        this.submitted = false;
        this.validationMessages = {
            'types': {
                'required': '此为必填项，不能为空',
                'minlength': '名称不能少于4个字符'
            },
            'brand': {
                'required': '此为必填项，不能为空',
            },
            'new_style': {
                'required': '国内新型号，必填，方便搜索'
            },
            'old_style': {
                'required': '国内旧型号，必填，方便搜索'
            },
            'inside_diameter': {
                'required': '内径，填写具体数值即可，方便搜索，不能为空'
            },
            'outside_diameter': {
                'required': '外径，填写具体数值即可，方便搜索，不能为空'
            },
            'thickness': {
                'required': '厚度，填写具体数值即可，方便搜索，不能为空'
            },
            'purchase': {
                'required': '此为必填项，不能为空且只能输入数字'
            },
            'unit': {
                'required': '此为必填项，不能为空只能输入数字'
            },
            'stock': {
                'required': '此为必填项，不能为空只能输入数字'
            }
        };
        this.formErrors = {
            'types': '',
            'brand': '',
            'new_style': '',
            'old_style': '',
            'inside_diameter': '',
            'outside_diameter': '',
            'thickness': '',
            'purchase': '',
            'unit': '',
            'stock': ''
        };
    }
    Object.defineProperty(BearingAddComponent.prototype, "routeAnimation", {
        /**
         * 路由转场动画
         */
        get: function () {
            return true;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BearingAddComponent.prototype, "display", {
        get: function () {
            return 'block';
        },
        enumerable: true,
        configurable: true
    });
    BearingAddComponent.prototype.onSubmit = function () { this.submitted = true; };
    ;
    BearingAddComponent.prototype.addBearing = function () {
        var _this = this;
        console.log(this.bear);
        this.active = false;
        setTimeout(function () { return _this.active = true; }, 0);
    };
    ;
    Object.defineProperty(BearingAddComponent.prototype, "diagnostic", {
        get: function () { return JSON.stringify(this.bear); },
        enumerable: true,
        configurable: true
    });
    ;
    BearingAddComponent.prototype.ngOnInit = function () {
        this.buildForm();
    };
    BearingAddComponent.prototype.buildForm = function () {
        var _this = this;
        this.bearingForm = this.fb.group({
            'types': [
                this.bear.types, [
                    forms_1.Validators.required,
                    forms_1.Validators.minLength(4)
                ]
            ],
            'brand': [
                this.bear.brand, [forms_1.Validators.required]
            ],
            'new_style': [
                this.bear.new_style, [forms_1.Validators.required]
            ],
            'old_style': [
                this.bear.old_style, [forms_1.Validators.required]
            ],
            'inside_diameter': [
                this.bear.inside_diameter, [forms_1.Validators.required]
            ],
            'outside_diameter': [
                this.bear.outside_diameter, [forms_1.Validators.required]
            ],
            'thickness': [
                this.bear.thickness, [forms_1.Validators.required]
            ],
            'purchase': [
                this.bear.purchase_price, [forms_1.Validators.required]
            ],
            'unit': [
                this.bear.unit_price, [forms_1.Validators.required]
            ],
            'stock': [this.bear.stock]
        });
        this.bearingForm.valueChanges.subscribe(function (data) { return _this.onValueChanged(data); });
        this.onValueChanged();
    };
    BearingAddComponent.prototype.onValueChanged = function (data) {
        if (!this.bearingForm) {
            return;
        }
        var form = this.bearingForm;
        for (var field in this.formErrors) {
            this.formErrors[field] = '';
            var control = form.get(field);
            if (control && control.dirty && !control.valid) {
                var messages = this.validationMessages[field];
                for (var key in control.errors) {
                    this.formErrors[field] += messages[key] + ' ';
                }
            }
        }
    };
    __decorate([
        core_1.HostBinding('@routeAnimation'), 
        __metadata('design:type', Object)
    ], BearingAddComponent.prototype, "routeAnimation", null);
    __decorate([
        core_1.HostBinding('style.display'), 
        __metadata('design:type', Object)
    ], BearingAddComponent.prototype, "display", null);
    BearingAddComponent = __decorate([
        core_1.Component({
            moduleId: module.id,
            selector: 'my-bearing-add',
            templateUrl: 'bearing-add.component.html',
            styleUrls: ['bearing.css'],
            animations: [
                core_1.trigger('routeAnimation', [
                    core_1.state('*', core_1.style({
                        opacity: 1,
                        transform: 'translateX(0)'
                    })),
                    core_1.transition('void => *', [
                        core_1.style({
                            opacity: 0,
                            transform: 'translateX(-15%)'
                        }),
                        core_1.animate('0.35s ease-in-out')
                    ])
                ])
            ]
        }), 
        __metadata('design:paramtypes', [forms_1.FormBuilder])
    ], BearingAddComponent);
    return BearingAddComponent;
}());
exports.BearingAddComponent = BearingAddComponent;
